package cn.xxstudy.storage

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView

class GalleryAdapter(private val list: ArrayList<Uri>) : RecyclerView.Adapter<GalleryAdapter.GalleryHolder>() {

    fun notify(list: ArrayList<Uri>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
        return GalleryHolder(view)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: GalleryHolder, position: Int) {
        holder.img.setImageURI(list[position])
    }

    class GalleryHolder(view: View) : RecyclerView.ViewHolder(view) {
        val img: ImageView = view.findViewById(R.id.img)
    }
}